﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

public class Building : ISerializable
{
	// Declaration
	private int m_FacilityID;
	private GameEnums.Facilities m_Name;					// name of this facility
	private string m_DisplayName;									// display name of this facility
	private GameEnums.TechName[] m_MotherTech;		// discovered by which parent technology
	private double m_TurnCost;											// cost per turn, in terms of productivity unit
	private double m_BuildCost;											// cost to build in terms of productivity unit

	private GameEnums.Facilities[] m_Obsolete;		// GameEnums.Facilities to be obsoleted due to building this powerhouse
	private Planet m_Planet;											// planet of this building
	private bool m_GetID = false;

	private Item[] m_Unlocked;											// items can be discovered by building this powerhouse
	private Item[] m_CanCraft;											// items can be crafted by this powerhouse


	// ISerializable
	public ulong Serial
	{
		get { return (ulong)m_FacilityID; }
	}

	#region getters and setters
	public int FacilityID
	{
		get { return m_FacilityID; }
		set { m_FacilityID = value; }
	}

	public string DispName
	{
		get { return m_DisplayName; }
		set { m_DisplayName = value; }
	}

	public double BuildCost
	{
		get { return m_BuildCost; }
		set { m_BuildCost = value; }
	}

	public GameEnums.Facilities Name
	{
		get { return m_Name; }
		set { m_Name = value; }
	}

	public GameEnums.TechName[] MotherTech
	{
		get { return m_MotherTech; }
		set { m_MotherTech = value; }
	}

	public double TurnCost
	{
		get { return m_TurnCost; }
		set { m_TurnCost = value; }
	}

	public Item[] Unlocked
	{
		get { return m_Unlocked; }
		set { m_Unlocked = value; }
	}

	public Item[] CanCraft
	{
		get { return m_CanCraft; }
		set { m_CanCraft = value; }
	}

	public GameEnums.Facilities[] Obsolete
	{
		get { return m_Obsolete; }
		set { m_Obsolete = value; }
	}

	public Planet Planet
	{
		get { return m_Planet; }
		set { m_Planet = value; }
	}

	public bool GetID
	{
		get { return m_GetID; }
		set { m_GetID = value; }
	}
	#endregion


	public Building()
	{
		// initialize arrays
		m_MotherTech = new GameEnums.TechName[] { };

		m_Unlocked = new Item[] { };
		m_CanCraft = new Item[] { };
		m_Obsolete = new GameEnums.Facilities[] { };

		m_BuildCost = 200.0;
		m_Planet = null;

	}


	// ISerializable
	public virtual void Serialize(GenericWriter writer)
	{
		writer.Write((int)0); // version

		writer.Write((int)m_FacilityID);
		writer.Write((int)m_Name);
		writer.Write((string)m_DisplayName);

		writer.Write((int)m_MotherTech.Length);
		if (m_MotherTech.Length > 0)
		{
			foreach (GameEnums.TechName tn in m_MotherTech)
				writer.Write((int)tn);
		}

		writer.Write((double)m_TurnCost);
		writer.Write((double)m_BuildCost);

		writer.Write((int)m_Obsolete.Length);
		if (m_Obsolete.Length > 0)
		{
			foreach (GameEnums.Facilities ft in m_Obsolete)
				writer.Write((int)ft);
		}

		writer.Write((Planet)m_Planet);

	}

	public virtual void Deserialize(GenericReader reader)
	{
		int version = reader.ReadInt();

		m_FacilityID = reader.ReadInt();
		m_Name = (GameEnums.Facilities)reader.ReadInt();
		m_DisplayName = reader.ReadString();

		int count = reader.ReadInt();
		if (count > 0)
			for (int i = 0; i < count; i++)
				m_MotherTech[i] = (GameEnums.TechName)reader.ReadInt();

		m_TurnCost = reader.ReadDouble();
		m_BuildCost = reader.ReadDouble();

		count = reader.ReadInt();
		if (count > 0)
			for (int i = 0; i < count; i++)
				m_Obsolete[i] = (GameEnums.Facilities)reader.ReadInt();

		m_Planet = reader.ReadPlanet();

	}

	public virtual void OnBeginTurn()
	{
	}

	public virtual void OnTurn()
	{
	}

	// for static version buildings
	public virtual void OnTurn(Planet pl)
	{
	}

	public virtual void OnEndTurn()
	{
	}

	public virtual void OnBuilt()
	{

	}

	// for static version buildings
	public virtual void OnBuilt(Planet pl)
	{

	}

	public virtual void OnRemove()
	{
	}

	// static methods
	public static void OnRemove(Planet pl, Building bd)
	{
		//if (pl == null || bd == null)
		//  return;

		//pl.RemoveFacility(bd);
	}
}